//
//  WSHistoryViewController.m
//  GroupFourFM
//
//  Created by 王鑫 on 15/12/22.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSHistoryViewController.h"
#import "WSFindCell.h"
#import "WSPlayViewController.h"

@interface WSHistoryViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic,retain)WSBaseTableView *historyTableView;
@property(nonatomic,retain)NSMutableArray *historyArray;

@end

@implementation WSHistoryViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[WSFMData shareFMDB] openDBandCreate];
    self.historyArray = [NSMutableArray arrayWithArray:[[WSFMData shareFMDB] selectTrackData]];
    [self.historyTableView reloadData];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    //self.historyTableView.backgroundColor = [UIColor whiteColor];
}

- (void)createTableView
{
    self.historyTableView = [[WSBaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT_TABLE) style:UITableViewStylePlain];
    [self.view addSubview:self.historyTableView];
    self.historyTableView.dataSource = self;
    self.historyTableView.delegate = self;
    [self.historyTableView reloadData];
    [self.historyTableView registerClass:[WSFindCell class] forCellReuseIdentifier:@"WSFindCell"];
    
    // 取消cell之间的分割线
    self.historyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark 设置tableview区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _historyArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WSFindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WSFindCell"];
    
    WSChildListModel *model = [self.historyArray objectAtIndex:indexPath.row];
    
    cell.model = model;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_WIDTH*0.24;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60;
}

#warning 点击cell的方法未实现
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WSChildListModel *model = self.historyArray[indexPath.row];
    WSPlayViewController *player = [WSPlayViewController sharaMusicPlayer];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
    player.song_id = model.trackId;
    [self presentViewController:naVC animated:YES completion:^{
        
    }];
}

#pragma mark - 自定义tableview的头部区域
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    NSLog(@"tabeleheader");
    
    UIView *headerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.06)];
    UIImageView *deleteImage = [[UIImageView alloc] init];
    deleteImage.image = [UIImage imageNamed:@"iconfont-shanchu-2"];
    [headerV addSubview:deleteImage];
    
    [deleteImage mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(@115);
        make.top.mas_equalTo(@18);
        make.width.height.mas_equalTo(@24);
        
    }];
    
    
    WSBaseBtn *headBtn = [WSBaseBtn buttonWithType:UIButtonTypeCustom];
    [headerV addSubview:headBtn];
    
    [headBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(@140);
        make.right.mas_equalTo(@-140);
        make.top.mas_equalTo(@10);
        make.bottom.mas_equalTo(@-10);
        
    }];
    
    headBtn.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    [headBtn setTitle:@"清空历史记录" forState:UIControlStateNormal];
    [headBtn setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
    [headBtn.titleLabel setFont:[UIFont systemFontOfSize:14.5f]];
    [headBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return headerV;
}

#pragma mark - 清空按钮的点击方法
- (void)btnAction:(UIButton *)btn
{
    [[WSFMData shareFMDB] dropTableTracks];
    self.historyArray = [NSMutableArray array];
    [self.historyTableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
